package pancake.function;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.Spinner;

public class MyAccount  extends Activity {
	/** Called when the activity is first created. */
    private Button btnAddAccount,btnDelAccount;
    private EditText txtUsr,txtPwd;
    private Spinner cmbType;
    private ListView lstAccountView;
	@Override
    public void onCreate(Bundle savedInstanceState) {  
		super.onCreate(savedInstanceState);
		setContentView(R.layout.listaccount);
           this.btnAddAccount = (Button)findViewById(R.id.btnAccountAdd);
           this.lstAccountView = (ListView)findViewById(R.id.lstAccount);
           this.btnDelAccount = (Button)findViewById(R.id.btnAccountDelete);
           this.InitEvent();
           this.ListAccount(this);
    }
	private void InitEvent()
	{
		this.btnAddAccount.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				ShowDialog(v);
			}
		});
		this.btnDelAccount.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Delete();
			}
		});
	}
	private void Delete()
	{
		String item = this.lstAccountView
							.getItemAtPosition(this.lstAccountView
											.getSelectedItemPosition()).toString();
		String[] values = item.split(":");
		DbAccount db = new DbAccount(this);
		db.Delete(values[0]);
	}
    private void ShowDialog(View v)
    {
    	final Context context = this;
    	LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		final View vPopupWindow = inflater.inflate(R.layout.accountadd,null,false);
	 
		final PopupWindow dialog = new PopupWindow(vPopupWindow,300,300,true);
		Button btnCancel=(Button)vPopupWindow.findViewById(R.id.btnAccountClose); 
		Button btnSave=(Button)vPopupWindow.findViewById(R.id.btnAccountSave); 
		
		txtUsr = (EditText)vPopupWindow.findViewById(R.id.txtAccountName);
		txtPwd = (EditText)vPopupWindow.findViewById(R.id.txtAccountPwd);
		cmbType = (Spinner)vPopupWindow.findViewById(R.id.cmbType);
		 setTheme(android.R.style.Theme_Dialog);
		String[] type = {"饭否","Twitter"};
        cmbType.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,type));
        cmbType.setSelection(0);
      //显示popupWindow对话框  
        dialog.showAtLocation(v, Gravity.CENTER, 0, 0);
       
        btnCancel.setOnClickListener(new View.OnClickListener(){  
            @Override  
            public void onClick(View v) {  
            	dialog.dismiss();//关闭  
            }  
        }); 
        btnSave.setOnClickListener(new View.OnClickListener() {  
			@Override  
			public void onClick(View v) 
            { 
 
				String type = cmbType.getSelectedItem().toString();
				String user = txtUsr.getText().toString();
				String pwd = txtPwd.getText().toString();
				DbAccount db = new DbAccount(user,pwd,type,context);
				if(db.IsValid())
				{
					db.Insert();
					ListAccount(context);
					dialog.dismiss();//关闭  
				}
				
            }
        });
    }
    private void ListAccount(Context context)
    {
    	DbAccount db = new DbAccount(context);
    	String[] items = db.GetAccounts();
    	if(items != null)
    	{
	    	ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, 
				android.R.layout.simple_list_item_1, items);
	    	lstAccountView.setAdapter(adapter);
    	}
    	
    }

}
